package h0908;
/**
  * @description 岛屿数量
  * @author 不知名帅哥
  * @date 2024/9/8 22:01
  * @version 1.0
*/
public class TheNumberOfIslands {
    public static void main(String[] args) {

    }
    public int numIslands(char[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int count=0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j]=='1'){
                    count++;
                    dfs(grid,i,j,m,n);
                }
            }
        }
        return count;
    }

    private void dfs(char[][] grid, int i, int j,int m,int n) {
        if (i<0||i>=m||j<0||j>=n||grid[i][j]=='0') return;
        grid[i][j]='0';
        dfs(grid, i-1, j, m, n);
        dfs(grid, i+1, j, m, n);
        dfs(grid, i, j-1, m, n);
        dfs(grid, i, j+1, m, n);
    }
}
